package com.songyuquan.webcs.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 配置图像拦截器
 */
public class ImgInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String path = request.getServletPath();
        String substring = path.substring(path.lastIndexOf('.') + 1);
        if ("jpg".equals(substring)) {
            response.setContentType("image/jpeg");
        } else if ("png".equals(substring)) {
            response.setContentType("image/png");
        } else if ("gif".equals(substring)) {
            response.setContentType("image/gif");
        } else {
            response.setContentType("image/jpeg");
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
